﻿Public Class FrmNhaCungCap
    Private selectedMaNCC As String
    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        btnSua.Text = "&Sửa"
        txtDiaChi.Text = ""
        txtTenNCC.Text = ""
        txtDienThoai.Text = ""
    End Sub

    Private Sub FrmNhaCungCap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvNcc.DataSource = NhaCungCap.DanhSach()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        btnSua.Text = "&Sửa"
        If txtTenNCC.Text <> "" And txtDiaChi.Text <> "" And txtDienThoai.Text <> "" Then
            If Not NhaCungCap.Them(txtTenNCC.Text, txtDiaChi.Text, txtDienThoai.Text) Then
                MsgBox("Không thể thêm dữ liệu!", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvNcc.DataSource = NhaCungCap.DanhSach()
            btnNhapLai_Click(sender, e)
        Else
            MsgBox("Bạn cần nhập đầy đủ các trường thông tin!", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "&Sửa" Then
            selectedMaNCC = dgvNcc.CurrentRow.Cells("MaNCC").Value
            txtTenNCC.Text = dgvNcc.CurrentRow.Cells("TenNCC").Value
            txtDiaChi.Text = dgvNcc.CurrentRow.Cells("DiaChi").Value
            txtDienThoai.Text = dgvNcc.CurrentRow.Cells("DienThoai").Value
            btnSua.Text = "&Lưu"
        Else

            If Not NhaCungCap.CapNhat(selectedMaNCC, txtTenNCC.Text, txtDiaChi.Text, txtDienThoai.Text) Then
                MsgBox("Không thể lưu dữ liệu", MsgBoxStyle.Critical, "Lỗi")
            End If
            btnSua.Text = "&Sửa"
            dgvNcc.DataSource = NhaCungCap.DanhSach()
            btnNhapLai_Click(sender, e)
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        btnSua.Text = "&Sửa"
        If MsgBox("Bạn có chắc chắn muốn xóa nhà cung cấp " & dgvNcc.CurrentRow.Cells("TenNCC").Value & " không?", MsgBoxStyle.YesNo, "Thông tin") = DialogResult.Yes Then
            If Not NhaCungCap.Xoa(dgvNcc.CurrentRow.Cells("MaNCC").Value) Then
                MsgBox("Không thể xóa dữ liệu", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvNcc.DataSource = NhaCungCap.DanhSach()
        End If
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        btnSua.Text = "&Sửa"
        dgvNcc.DataSource = NhaCungCap.TimKiem(txtTuKhoa.Text)
    End Sub

    Private Sub dgvNcc_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvNcc.CellDoubleClick
        btnSua.Text = "&Sửa"
        btnSua_Click(sender, e)
    End Sub

    Private Sub btndaydu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndaydu.Click
        btnSua.Text = "&Sửa"
        dgvNcc.DataSource = NhaCungCap.DanhSach()
    End Sub

    Private Sub txtDienThoai_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDienThoai.KeyPress
        NumberOnly(sender, e)
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Hide()
        Me.Close()
    End Sub
End Class